﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace ACEPAPI
{
    public class Utils
    {
        /// <summary>
        /// 把字符串转换成byte[]
        /// 0x开头的行转换成命令
        /// </summary>
        /// <param name="dataString">包含命令、参数与内容的字符串</param>
        /// <returns></returns>
        public static byte[] ConvertToBytes(string dataString)
        {
            List<byte> lbyte = new List<byte>();
            string[] lines = dataString.Split('\n');
            for (int i = 0; i < lines.Length; i++)
            {
                //0x开头的行处理成命令
                if (lines[i].StartsWith("0x"))
                {
                    string[] lsp = lines[i].Split(' ');
                    for (int j = 0; j < lsp.Length; j++)
                    {
                        if (lsp[j].StartsWith("0x"))
                        {
                            lbyte.Add(Convert.ToByte(Convert.ToInt32(lsp[j].Replace("0x", "").Trim(), 16)));
                        }
                        else
                        {
                            lbyte.Add(Convert.ToByte(lsp[j].Trim()));
                        }
                    }
                }
                else
                {
                    lbyte.AddRange(Encoding.ASCII.GetBytes(lines[i]));
                }
            }
            return lbyte.ToArray();
        }
    }
}
